package com.xywang.service.user;

import com.xywang.dao.BaseDao;
import com.xywang.dao.user.UserDao;
import com.xywang.dao.user.UserDaoImpl;
import com.xywang.pojo.User;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

// 通过用户名密码将数据库的某一行数据取出，存入user对象
public class UserServiceImpl implements UserService{
    // 业务层都会调用dao层，所以我们要引入Dao层；
    private UserDao userDao;
    public UserServiceImpl(){
        userDao = new UserDaoImpl();
    }
    // 用户登录，验证密码
    @Override
    public User login(String userCode, String userPassword){
        Connection connection = null;
        User user = null;

        try {
            connection = BaseDao.getConnection();
            //通过业务层调用对应的具体的数据库操作
            user = userDao.getLoginUser(connection, userCode);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(connection, null, null);
        }
        // 密码判断，如果密码不一致，则设user为空，即登录失败
        if(user!=null){
            if(!user.getUserPassword().equals(userPassword)){
                user = null;
            }
        }
        return user;
    }

    // 用户修改密码
    @Override
    public boolean updatePwd(int id, String password) {
        Connection connection = null;
        boolean flag = false;
        // 修改密码
        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);
            int i = userDao.updatePwd(connection, id, password);
            connection.commit();
            // System.out.println("UserServiceImpl:: "+password); 排错时用的
            if(i>0){
                flag = true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }

    // 根据用户或角色查询用户的记录总数
    @Override
    public int getUserCount(String username, int userRole) {
        Connection connection = null;
        int userCount = 0;

        try {
            connection = BaseDao.getConnection();
            userCount = userDao.getUserCount(connection, username, userRole);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            BaseDao.closeResource(connection,null,null);
        }
        // System.out.println("UserServiceImpl-->getUserCount:"+userCount);
        return userCount;
    }

    // 根据条件查询用户列表，有分页
    @Override
    public List<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {
        Connection connection = null;
        List<User> userList = null;
        try {
            connection = BaseDao.getConnection();
            userList = userDao.getUserList(connection, queryUserName, queryUserRole, currentPageNo, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return userList;
    }

    // 添加用户
    @Override
    public boolean addUserService(User user) {
        Connection connection = null;
        boolean flag = false;

        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);// 关闭自动提交，开启事务
            int i = userDao.addUserDao(connection, user);
            connection.commit();
            if(i>0){
                flag = true;
                System.out.println("添加成功");
            }else{
                System.out.println("添加失败");
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                System.out.println("rollback!!");
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }

    // 添加用户时判断userCode是否存在
    @Override
    public User selectUserCode(String userCode) {
        Connection connection = null;
        User user = null;

        try {
            connection = BaseDao.getConnection();
            user = userDao.getLoginUser(connection, userCode);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(connection, null, null);
        }
        return user;
    }

    // 删除用户
    @Override
    public boolean deleteUserService(int id) {
        Connection connection = null;
        boolean flag = false;

        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);// 关闭自动提交，开启事务

            int i = userDao.deleteUser(connection, id);
            connection.commit();
            if(i>0){
                flag = true;
                System.out.println("添加成功");
            }else{
                System.out.println("添加失败");
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                System.out.println("rollback!!");
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }

    // 通过id得到用户
    @Override
    public User getUserByIdService(int id) {
        Connection connection = null;
        User userById = null;
        try {
            connection = BaseDao.getConnection();
            userById = userDao.getUserById(connection, id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(connection, null, null);
        }
        return userById;
    }

    // 修改用户
    @Override
    public boolean updateUserService(User user) {
        Connection connection = null;
        boolean flag = false;

        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);// 关闭自动提交，开启事务
            int i = userDao.updateUser(connection, user);
            connection.commit();
            if(i>0){
                flag = true;
                System.out.println("修改用户信息成功！");
            }else{
                System.out.println("修改用户信息失败！");
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                System.out.println("修改用户信息错误rollback!!");
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }


    @Test
    public void test(){
        UserServiceImpl userService = new UserServiceImpl();
        List<User> users = userService.getUserList(null,0, 1, 5);
        int userCount = userService.getUserCount("赵", 0);
        System.out.println(userCount);
        for (User a :users) {
            System.out.println(a.toString());
        }
    }

}
